package com.nulldev.util.net.dsap.packets;

import com.nulldev.util.net.dsap.Client;
import com.nulldev.util.net.dsap.Server;
import com.nulldev.util.net.packets.APILevel;
import com.nulldev.util.net.packets.NetInfo;
import com.nulldev.util.net.packets.PacketManager;
import com.nulldev.util.net.packets.UniversalPacket;
import com.nulldev.util.other.BooleanUtil;

@APILevel(level = 4)
/**
 * DSAP (Dynamic Socket Allocation Protocol) <br>
 * Permission Check <br>
 * Checks if the server supports DSAP and if so then check if Encrypted Streams are supported
 * @author null
 * @since nullUtil 2.6-beta b211
 */
public class DSAPAllowed extends UniversalPacket {
	
	public DSAPAllowed() {
		super("Dynamic Socket Allocation Protocol: Permission Check", "dsap.check", "");
	}

	@Override
	public void client(byte[] data, byte[] raw, NetInfo client) {
		if (client.get() instanceof Client) {
			final Client cl = (Client) client.get();
			cl.setDSAPSupport(BooleanUtil.safeParse(new String(data)));
		}else {
			client.invoke("dsap.isSupported", BooleanUtil.safeParse(new String(data)));
		}
	}

	@Override
	public void server(byte[] data, byte[] raw, NetInfo connection) {
		if (connection.get() instanceof Server) {
			final Server obj = (Server) connection.get();
			connection.reply(PacketManager.fromID("dsap.check", String.valueOf(obj.isDSAPready())));
		}else {
			connection.reply(PacketManager.fromID("dsap.check", "false"));
		}
	}

}
